﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 848.字母移位
    /// </summary>
    class LeetCode848: LeetCodeBase
    {
        public override void Execute()
        {
            CommonFuntions.Print(ShiftingLetters("abc", new int[] { 3, 5, 9 }));
            // a : 3 + 5 + 9
            // b : 3 + 5
            // c : 3

            CommonFuntions.Print(ShiftingLetters
                (
                "mkgfzkkuxownxvfvxasy",
                new int[] { 505870226, 437526072, 266740649, 224336793, 532917782, 311122363, 567754492, 595798950, 81520022, 684110326, 137742843, 275267355, 856903962, 148291585, 919054234, 467541837, 622939912, 116899933, 983296461, 536563513 }
                ));
        }

        public string ShiftingLetters(string s, int[] shifts)
        {
            StringBuilder sb = new StringBuilder(s);
            long offset = 0;
            for (int i = shifts.Length - 1; i > -1; i--) //统计每个字母要移位的次数
            {
                offset += shifts[i];
                CommonFuntions.Print(offset);
                sb[i] = ShiftLetter(sb[i], offset);
            }

            return sb.ToString();
        }

        public char ShiftLetter(char letter, long offset)
        {
            offset %= 26;

            if (letter + offset > 122)
            {
                return (char)(letter + offset - 26);
            }

            return (char)(letter + offset);
        }

        public string ReplaceString(string source, char key, int index)
        {
            char[] chs = new char[source.Length];
            for (int i = 0; i < source.Length; i++)
            {
                if (i == index)
                {
                    chs[i] = key;
                }
                else
                {
                    chs[i] = source[i];
                }
            }

            return new string(chs);
        }
    }
}
